﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ImageProcessing;
using System.IO;
using System.Drawing;
using System.Net;

namespace Image_Processing_Website
{
    public partial class ImageViewer : System.Web.UI.Page
    {
        #region private members

        private List<String> m_imageURLs = null;
        private ImageHandler imageHandler = new ImageHandler();

        #endregion

        #region protected methods
        protected override void OnInit(EventArgs e)
        {
            m_imageURLs = new List<String>();
            //m_imageURLs.Add(@"http://www.clipart.com/images/guest/flash/200710/image_02.jpg");
            //m_imageURLs.Add(@"http://i.minus.com/ibzIMXKdIUiqZL.jpg");
            //m_imageURLs.Add(@"http://i.minus.com/i8kMJZ3YvXNaK.jpg");
            //m_imageURLs.Add(@"http://i.minus.com/iWj5GjCUsDyLa.jpg");
            //m_imageURLs.Add(@"http://i.minus.com/ipFnPAltzV96v.jpg");
            //m_imageURLs.Add(@"http://i.minus.com/ibvAHxLlUl8bMy.jpg");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
            DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("/Images/test/"));
            List<FileInfo> lastUpdateFile = Utilities.GetLastUpdatedFileInDirectory(directoryInfo);
            m_imageURLs.Add("/Images/test/"+lastUpdateFile[lastUpdateFile.Count-1].Name);
            imgProcessed.ImageUrl = m_imageURLs[0];
            
            if (Request.Params["process"] == null || Request.Params["value"] == null) return;
            
            var process = Request.Params["process"];
            var value = Request.Params["value"];
            if (process != null && value != null)
            {
               imgProcessed.ImageUrl = ProcessImage(m_imageURLs[0], process, value);
            }

        }

        #endregion

        #region private methods        

        private RotateFlipType setRotateFlipType(String value)
        {
            RotateFlipType rotateFlipType = new RotateFlipType();
            if (value.Equals("Rotate90FlipNone"))
            {
                rotateFlipType = RotateFlipType.Rotate90FlipNone;
            }
            else if (value.Equals("Rotate180FlipNone"))
            {
                rotateFlipType = RotateFlipType.Rotate180FlipNone;
            }
            else if (value.Equals("Rotate270FlipNone"))
            {
                rotateFlipType = RotateFlipType.Rotate270FlipNone;
            }
            else if (value.Equals("RotateNoneFlipX"))
            {
                rotateFlipType = RotateFlipType.RotateNoneFlipX;
            }
            else if (value.Equals("Rotate90FlipX"))
            {
                rotateFlipType = RotateFlipType.Rotate90FlipX;
            }
            else if (value.Equals("Rotate180FlipX"))
            {
                rotateFlipType = RotateFlipType.Rotate180FlipX;
            }
            else if (value.Equals("Rotate270FlipX"))
            {
                rotateFlipType = RotateFlipType.Rotate270FlipX;
            }
            else if (value.Equals("RotateNoneFlipY"))
            {
                rotateFlipType = RotateFlipType.RotateNoneFlipY;
            }
            else if (value.Equals("Rotate90FlipY"))
            {
                rotateFlipType = RotateFlipType.Rotate90FlipY;
            }
            else if (value.Equals("Rotate180FlipY"))
            {
                rotateFlipType = RotateFlipType.Rotate180FlipY;
            }
            else if (value.Equals("Rotate270FlipY"))
            {
                rotateFlipType = RotateFlipType.Rotate270FlipY;
            }
            else if (value.Equals("RotateNoneFlipXY"))
            {
                rotateFlipType = RotateFlipType.RotateNoneFlipXY;
            }
            else if (value.Equals("Rotate90FlipXY"))
            {
                rotateFlipType = RotateFlipType.Rotate90FlipXY;
            }
            else if (value.Equals("Rotate180FlipXY"))
            {
                rotateFlipType = RotateFlipType.Rotate180FlipXY;
            }
            else if (value.Equals("Rotate270FlipXY"))
            {
                rotateFlipType = RotateFlipType.Rotate270FlipXY;
            }
            else {return RotateFlipType.RotateNoneFlipNone; }
            return rotateFlipType;
        }

        private String ProcessImage(String imageURL, String process, String value)
        {
            String path = "";
            imageURL = Server.MapPath(imageURL);
            FileStream fileStream = new FileStream(imageURL, FileMode.Open);
            imageHandler.CurrentBitmap = new Bitmap(fileStream);
            

            switch (process)
            {
                case "brightness": imageHandler.SetBrightness(Int32.Parse(value)); break;
                case "contrast": imageHandler.SetContrast(Int32.Parse(value)); break;
                case "grayscale": imageHandler.SetGrayscale(); break;
                case "invest": imageHandler.SetInvert(); break;
                case "resize":
                                {
                                    String[] toIntValue = value.Split('x');
                                    imageHandler.Resize(Int32.Parse(toIntValue[0]), Int32.Parse(toIntValue[1]));
                                    break; 
                                }
                case "rotateflip":
                                {
                                    RotateFlipType rotateFlipType = setRotateFlipType(value);
                                    imageHandler.RotateFlip(rotateFlipType);
                                    break;
                                }
                default: return "/Images/test/" + Utilities.getImageName(imageURL);
            }

            fileStream.Close();

            path = Server.MapPath("/Images/test/") + process + value + "." + Utilities.getImageType(imageURL);
            imageHandler.SaveBitmap(path);

            path = "/Images/test/" + process + value +"." + Utilities.getImageType(imageURL);
            return path;
        }

        #endregion
    }
}